
/**
 * approve && reject
 * 驳回、通过组件
 * by mumu
 */

import React from 'react';
import './index.less';
import CommonModal from '../../components/CommonModal/index.jsx';
import {
    onWorkflowApprove,
    onWorkflowReject,
} from './index.config.js';
import {vendorsDanger} from '../../utils/config.js';
import { Checkbox } from 'tinper-bee';

class PassRejectModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            showModal: false, // 审批modal
            showPersonModal: false, // 指派人modal
            originData: null, // 原始数据
            afterAjaxResult: null, // 审批后返回的数据 
            assignPersonData: [], // 指派人数据
            opnionText: '', // 审批意见
        };
        this.output = {
            initPassRejectModal: this.initPassRejectModal,
            closePassRejectModal: this.closePassRejectModal,
            handleBillPass: this.handlePass,
        }
    }
    componentDidMount() {

    }

    // 关闭审批通过模态框
    closePassRejectModal = () => {
        this.setState({ showModal: false });
    }

    // 关闭指派人弹窗
    closeApproveModal = () => {
        this.setState({ showPersonModal: false });
    }

    // 显示页面，存储当前操作数据
    initPassRejectModal = (data) => {
        this.setState({ showModal: true, originData: { ...data } });
    }

    // 渲染审批组件页面
    getModalContext = () => {
        let { assignPersonData } = this.state;
        return assignPersonData ? (<div>
            <textarea className="opinion--box" onChange={this.handlerSaveOpnionText}></textarea>
        </div>) : ''
    }

    // 渲染指派人组件页面
    getPersonModalContext = () => {
        let { assignPersonData } = this.state;
        return assignPersonData ? (<div className="approval--person">
            <p className="approval--title">审批成员(多选)</p>
            <ul className="approval--list">
                {assignPersonData.map((item, index) => {
                    return <li key={item.id}>
                        <Checkbox checked={item.isChecked} onChange={() => this.changeCheck(index)}></Checkbox>
                        {item.name}
                    </li>
                })}
            </ul>
        </div>) : ''
    }

    // 选择审批人
    changeCheck = (i) => {
		let { assignPersonData } = this.state;
		assignPersonData[i].isChecked = !assignPersonData[i].isChecked;
        this.setState({ assignPersonData });
    }

    // 处理审批意见
    handlerSaveOpnionText = (e) => {
        let value = e.target.value;
        this.setState({
            opnionText: value
        });
    }

    // 审批 通过
    handlePass = () => {
        let { record, index, page} = this.state.originData;
        let postData = this.getPostData();
        onWorkflowApprove(postData).then((res) => {
            let { resultCode, result, exMsg } = res.data;
            this.setState({ showModal: false });
            if (resultCode === 'ok') {
                if (result === 'success') {
                    this.setState({ showModal: false, opnionText: '', showPersonModal: false, });
                    // record.billstatus = 9;
                    // record.strBillstatus = '审批通过';
                    this.props.handlePassReject({ record, index });
                } else if (result.assignInfoItems[0].participants.length === 1) {
                    onWorkflowApprove({ ...postData, assignFlag: true, assignInfo: result }).then((res) => {
                        let { resultCode, result, exMsg } = res.data;
                        if (resultCode === 'ok') {
                            this.setState({ showModal: false, opnionText: '' });
                            record.billstatus = 9;
                            record.strBillstatus = '审批通过';
                            this.props.handlePassReject({ record, index });
                        } else {
                            vendorsDanger({ text: exMsg })
                        }
                    });
                } else {
                    this.setState({
                        showModal: false,
                        showPersonModal: true,
                        afterAjaxResult: result,
                        assignPersonData: result.assignInfoItems[0].participants,
                    });
                }
            } else {
                vendorsDanger({ text: exMsg })
            }
        });
    }

    // 审批 驳回
    handleReject = () => {
        let { record, index, page} = this.state.originData;
        let postData = this.getPostData();
        onWorkflowReject(postData).then((res) => {
            let { resultCode, result, exMsg } = res.data;
            if(resultCode === 'ok') {
                this.setState({ showModal: false, opnionText: '' });
                record.billstatus = 8;
                record.strBillstatus = '已驳回';
                this.props.handlePassReject({ record, index });
            } else {
                vendorsDanger({ text: exMsg })
            }
        });
    }

    // 选择审批人
    handlConfirmSelectP = () => {
        this.handlePass();
    }

    // 获取审批请求数据
    getPostData = () => {
        let { record, index, page, primaryKey } = this.state.originData;
        let postData = {
            pkBill: record[primaryKey],
            billType: record.billType,
            tenantId: record.pkTenant,
            opinion: this.state.opnionText,
            assignFlag: this.state.afterAjaxResult ? true : false,
            assignInfo: this.state.afterAjaxResult
        }
        return postData;
    }

    render() {
        return (
            <div>
                <CommonModal show={this.state.showModal}
                    close={this.closePassRejectModal}
                    cancel={this.handleReject}
                    confirm={this.handlePass}
                    content={this.getModalContext()}
                    backdrop={true}
                    size='lg'
                    title='审批意见'
                    paramBtnContext={{ 'type-1': '驳回', 'type-2': '通过' }}
                    oneButton={false}
                    oneButtonText='确定'
                    paramBtnClass=''
                >
                </CommonModal>
                <CommonModal show={this.state.showPersonModal}
                    close={this.closeApproveModal}
                    confirm={this.handlConfirmSelectP}
                    content={this.getPersonModalContext()}
                    backdrop={true}
                    size='lg'
                    title='选择审批人'
                    oneButton={true}
                    oneButtonText='确定'
                    paramBtnClass=''
                >
                </CommonModal>
            </div>

        )
    }
}



export default function YGCreatePassRejectModal(params) {
    return (
        <PassRejectModal ref={child => this.passreject = child} {...params} />
    )
}